package cn.hty.mymsg.api.config;


import cn.hty.mymsg.api.action.recall.RecallAssembleAction;
import cn.hty.mymsg.api.action.recall.RecallMqAction;
import cn.hty.mymsg.api.action.send.SendAfterCheckAction;
import cn.hty.mymsg.api.action.send.SendAssembleAction;
import cn.hty.mymsg.api.action.send.SendMqAction;
import cn.hty.mymsg.api.action.send.SendPreCheckAction;
import cn.hty.mymsg.common.pipeline.ProcessController;
import cn.hty.mymsg.common.pipeline.ProcessTemplate;
import cn.hty.mymsg.service.enums.BusinessCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
@ComponentScan(basePackages = "cn.hty.mymsg")
public class PipelineConfig {
    @Autowired
    private SendPreCheckAction sendPreCheckAction;
    @Autowired
    private SendAssembleAction sendAssembleAction;
    @Autowired
    private SendAfterCheckAction sendAfterCheckAction;
    @Autowired
    private SendMqAction sendMqAction;

    @Autowired
    private RecallAssembleAction recallAssembleAction;
    @Autowired
    private RecallMqAction recallMqAction;
    //TODO 发送四个流程
    /**
     * 普通发送流程
     *      * 1. 前置参数校验
     *      * 2. 组装参数
     *      * 3. 后置参数校验
     *      * 4. 发送消息至MQ
     * @return
     */
    @Bean("commonSendTemplate")
    public ProcessTemplate commonSendTemplate(){
        ProcessTemplate processTemplate=new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(sendPreCheckAction,sendAssembleAction,sendAfterCheckAction,sendMqAction));
        return processTemplate;
    }

    /**
     * 消息撤回流程
     * 组装撤回参数，发送到mq
     * @return
     */
    @Bean("recallMessageTemplate")
    public ProcessTemplate recallMessageTemplate(){
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(recallAssembleAction, recallMqAction));
        return processTemplate;
    }

    /**
     * 流程控制器，将发送和撤回两种模式放入map传递给processController
     * @return
     */
    @Bean("apiProcessController")
    public ProcessController apiProcessController(){
        ProcessController processController = new ProcessController();
        Map<String, ProcessTemplate> templateConfig=new HashMap<>();
        templateConfig.put(BusinessCode.COMMON_SEND.getCode(),commonSendTemplate());
        templateConfig.put(BusinessCode.RECALL.getCode(),recallMessageTemplate());
        processController.setTemplateConfig(templateConfig);
        return processController;
    }
}
